<?php
/**
 * This file is accessed through JavaScript and executes code on the server side (AJAX).
 * Kontrolltoo save/load/delete functions.
 *
 * @author Siim Loog
 */

session_start();
require_once 'init.php';

if ( !isset($_POST['action']) ) {
	exit;
}

$t = new Test($sql);
$user = $_SESSION['userId'];
if(empty($_SESSION)){
	echo "LoggedOut";
	exit;
}

if(!isset($_SESSION['oppejoud'])){
		exit;
}


switch( $_POST['action'] ) {
	
	// Add new test to db
	
	case 'add':
		

		
		$kirjeldus = addslashes(trim($_POST['kirjeldus']));
		$nimi = addslashes(trim($_POST['nimi']));
		$avatud = addslashes(trim($_POST['avatud']));
		$katseid = addslashes(trim($_POST['katseid']));
		
		if ( Test::insert($sql, $katseid, $kirjeldus, $avatud, $nimi ) ) { 
			print "OK";
		} else {
			print "Failed";
		}	
		break;
	
	// Delete test with given id	
	case 'del':
		if(!isset($_SESSION['oppejoud'])){
				exit;
			}
		$id = (int)$_POST['id'];
		if ( $t->delete( $id, $user ) ) { 
			print "OK";
		} else {
			print "failed";
		}
		break;
	
	case 'testadder':
		echo $site->fetch( 'centerarea/addtest.html' );
		break;
		
	case 'listQuestions':
		$testId = $_POST['idkontrolltoo'];
		$result = Test::listQuestions($sql, $testId);
		$name = Test::getTestData($sql, $testId);
		$name = $name['kt_nimi'];
		
		$entryNr = 1;
		echo "<br/>";
		echo '<button onclick="addQuestion('.$testId.');" class="s11">Lisa ülesanne</button>';
		echo "<br/>";
		echo '<div class="header">'.$name.'</div>';
		if($result){
			foreach ($result as $value) {
				$identifier = "entity1_".strval($value['idulesanne']);
				$identifier2 = "entity2_".strval($value['idulesanne']);
				$identifier3 = "entity3_".strval($value['idulesanne']);
				//statusfield id
				$statusField = "answerStatus1_".strval($value['idulesanne']);
				$statusField2 = "answerStatus2_".strval($value['idulesanne']);
				$statusField3 = "answerStatus3_".strval($value['idulesanne']);
				//print_r($result);
				
				echo '<div class="question">
						<div class="questionDescription">
						<b>'.$entryNr.'.</b><br/>	
						<p>Ülesanne: ';
				echo '<input type="text" name="description"'.strval($value['idulesanne']).' id="description'.strval($value['idulesanne']).'" class="qEdit" value="'.$value['tekst'].'" /><br/>';
				echo "<br/>Argumendid: ";
				echo '<input type="text" name="arguments'.strval($value['idulesanne']).'" id="arguments'.$value['idulesanne'].'" class="qEdit" value="'.$value['argumendid'].'" /><br/>
						Punkte:
						<input type="number" min="1" max="10" step="1" value="5" name="points'.strval($value['idulesanne']).'" id="points'.strval($value['idulesanne']).'" /><br/>';
				echo '</p>
					</div>
						<div class="answerWrapper">
							<div class="correctAnswer"><p>
								Vastus 1:<br />
								<textarea spellcheck="false" rows="3" cols="50" oninput="emptyStatus('.$value['idulesanne'].',1);" name="'.$identifier.'" id = "'.$identifier.'">'.html_entity_decode($value['vastus']).'</textarea>
							</div>
							<div class="buttonPad">
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier.'\',\'∀\',\''.$statusField.'\');" class="s11">∀</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier.'\',\'∃\',\''.$statusField.'\');" class="s11">∃</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier.'\',\'¬\',\''.$statusField.'\');" class="s11">¬</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier.'\',\'⊃\',\''.$statusField.'\');" class="s11">⊃</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier.'\',\'⋁\',\''.$statusField.'\');" class="s11">⋁</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier.'\',\'&\',\''.$statusField.'\');" class="s11">&</button>
							</div>
							<div class="assessmentPanel">
							<button onclick="assessSyntax('.$value['idulesanne'].',1);" >Kontrolli süntaksi</button>
							<div class="answerStatus" id="'.$statusField.'"></div>
							</div>
						</div>
						
						<div class="answerWrapper">
							<div class="correctAnswer"><p>
								Vastus 2:<br />
								<textarea spellcheck="false" rows="3" cols="50" oninput="emptyStatus('.$value['idulesanne'].',2);"name="'.$identifier2.'" id = "'.$identifier2.'">'.html_entity_decode($value['vastus2']).'</textarea>
							</div>
							<div class="buttonPad">
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier2.'\',\'∀\',\''.$statusField2.'\');" class="s11">∀</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier2.'\',\'∃\',\''.$statusField2.'\');" class="s11">∃</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier2.'\',\'¬\',\''.$statusField2.'\');" class="s11">¬</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier2.'\',\'⊃\',\''.$statusField2.'\');" class="s11">⊃</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier2.'\',\'⋁\',\''.$statusField2.'\');" class="s11">⋁</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier2.'\',\'&\',\''.$statusField2.'\');" class="s11">&</button>
							</div>
							<div class="assessmentPanel">
							<button onclick="assessSyntax('.$value['idulesanne'].',2);" >Kontrolli süntaksi</button>
							<div class="answerStatus" id="'.$statusField2.'"></div>
							</div>
						</div>
						
						<div class="answerWrapper">
							<div class="correctAnswer"><p>
								Vastus 3:<br />
								<textarea spellcheck="false" rows="3" cols="50" oninput="emptyStatus('.$value['idulesanne'].',3);" name="'.$identifier3.'" id = "'.$identifier3.'">'.html_entity_decode($value['vastus3']).'</textarea>
							</div>
							<div class="buttonPad">
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier3.'\',\'∀\',\''.$statusField3.'\');" class="s11">∀</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier3.'\',\'∃\',\''.$statusField3.'\');" class="s11">∃</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier3.'\',\'¬\',\''.$statusField3.'\');" class="s11">¬</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier3.'\',\'⊃\',\''.$statusField3.'\');" class="s11">⊃</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier3.'\',\'⋁\',\''.$statusField3.'\');" class="s11">⋁</button>
								<button onclick="insertAtCursorAndUpdateStatus(\''.$identifier3.'\',\'&\',\''.$statusField3.'\');" class="s11">&</button>
							</div>
							<div class="assessmentPanel">
							<button onclick="assessSyntax('.$value['idulesanne'].',3);" >Kontrolli süntaksi</button>
							<div class="answerStatus" id="'.$statusField3.'"></div>
							</div>
						</div>
						<p>
						
						</p>
						<p>
						<button onclick="editProblem(';
				echo $value['idulesanne'];
				echo ');" class="answerButton">Muuda</button>
						<button onclick="removeProblem(';
				echo $value['idulesanne'].",".$testId;
				echo ');" class="answerButton">Kustuta</button>';
				echo "<br/>";
				echo "<br/>";
				echo '
				</p>
				</div></div>';
				
				
				$entryNr++;
			}
		}
		break;	
		
		print_r($result);
		break;
	case 'changeState':
		$testId = $_POST['idkontrolltoo']; 
		Test::changeState($sql, $testId);
		break;
		
	case 'getTestData':
		$testId = $_POST['idkontrolltoo'];
		$result = Test::getTestData($sql, $testId);
		
		echo '<p>
			Töö nimi:<br />
			<input type="text" name="testName" id="testName" class="test" value="';
		
		echo $result['kt_nimi'];
		echo '"/><br/>
		
			Kirjeldus:<br />
			<textarea rows="3" cols="50" name="testDescription" id = "testDescription">'.$result['kirjeldus'].'</textarea>';
		
		echo '<br/>

				<button onclick="alterTest('.$testId.');" class="userEditButton">Muuda</button><br/>
				<button onclick="listQuestions('.$testId.');" class="userEditButton">Vaata ülesandeid</button><br/>

				<button onclick="addQuestion('.$testId.');" class="userEditButton">Lisa ülesanne</button>
';
		
		break;
		
	case 'alterTest':
		$testId = $_POST['idkontrolltoo']; 
		$name = $_POST['name'];
		$description = $_POST['description'];
		  
		if(Test::alterTest($sql, $testId, $name, $description)){
			echo "OK";
		} else echo "Viga. Proovi hiljem uuesti";
		break;
		
	case 'deleteTest':
		$testId = $_POST['idkontrolltoo']; 
		  
		$result = Test::delete($sql, $testId);
		if($result){
			echo "OK";
		} else echo "Viga. Proovi hiljem uuesti";
		break;
		
		
}
?>
